﻿using System.IO.IsolatedStorage;
using Serialization;

namespace BingleMaps.Services
{
    public class LocalServices
    {
        public static void SaveLocalSetting(string key, object value)
        {
            if (value == null)
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(key);
                return;
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                IsolatedStorageSettings.ApplicationSettings[key] = SilverlightSerializer.Serialize(value);
            else IsolatedStorageSettings.ApplicationSettings.Add(key, SilverlightSerializer.Serialize(value));
        }

        public static object LoadLocalSetting(string key)
        {
            if (!IsolatedStorageSettings.ApplicationSettings.Contains(key)) return null;
            var value = IsolatedStorageSettings.ApplicationSettings[key];
            if (!(value is byte[])) return null;
            return SilverlightSerializer.Deserialize((byte[])IsolatedStorageSettings.ApplicationSettings[key]);
        }

        public static void ClearLocalSettings()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
        }

        public static bool HasLocalData
        {
            get { return IsolatedStorageSettings.ApplicationSettings.Count > 0; }
        }
    }
}
